using System;
using System.Collections.Generic;
using System.Text;

namespace BetaIto.ChaosDAV
{
    public enum GfsError
    {
        SUCCESS,
        UNKNOWN,
        SERVER_CONNECT_FAILED,
        INVALID_URL,
        INVALID_PROTOCOL,
        ERROR_RESPONSE_STATUS,
        INVALID_SSL_CERTIFICATE,
        UNKNOWN_RESPONSE_STATUS,
        WEBDAV_NOT_SUPPORTED,
        SERVER_NOT_ALLOW_PROPFIND,
        AUTHENTICATE_FAILED,
        UNKNOWN_RESPONSE_DATA,
        NOT_CONNECTED,
    }

    public class CfsErrorMessage
    {
        public CfsErrorMessage()
        {
            _list.Add(GfsError.SUCCESS, new GfsErrorData("Success."));
            _list.Add(GfsError.UNKNOWN, new GfsErrorData("Unknown error."));
            _list.Add(GfsError.SERVER_CONNECT_FAILED, new GfsErrorData("Server connect failed.", "Please check url and network. [Url={0}]"));
            _list.Add(GfsError.INVALID_URL, new GfsErrorData("Invalid URL.", "Please check url like \"http://example.com\". [Url={0}]"));
            _list.Add(GfsError.INVALID_PROTOCOL, new GfsErrorData("Invalid Protocol. Please check url like \"http://example.com\"."));
            _list.Add(GfsError.ERROR_RESPONSE_STATUS, new GfsErrorData("Server responsed error status. Please check url and permission."));
            _list.Add(GfsError.INVALID_SSL_CERTIFICATE, new GfsErrorData("Invalid SSL certificate on server. Please check certificate and regist certificate to OS."));
            _list.Add(GfsError.UNKNOWN_RESPONSE_STATUS, new GfsErrorData("Server responsed unknown status."));
            _list.Add(GfsError.WEBDAV_NOT_SUPPORTED, new GfsErrorData("Server not supported WebDAV protocol."));
            _list.Add(GfsError.SERVER_NOT_ALLOW_PROPFIND, new GfsErrorData("Server not allow PROPFIND method."));
            _list.Add(GfsError.AUTHENTICATE_FAILED, new GfsErrorData("Authenticate failed.", "Please check user name and password."));
            _list.Add(GfsError.UNKNOWN_RESPONSE_DATA, new GfsErrorData("Server responsed unknown data."));
            _list.Add(GfsError.NOT_CONNECTED, new GfsErrorData("Not connected."));
        }

        protected static readonly Dictionary<GfsError, GfsErrorData> _list = new Dictionary<GfsError, GfsErrorData>();
        protected static readonly CfsErrorMessage _message = new CfsErrorMessage();

        public struct GfsErrorData
        {
            public GfsErrorData(string message)
            {
                Message = message;
                Detail = null;
            }
            public GfsErrorData(string message, string detail)
            {
                Message = message;
                Detail = detail;
            }
            public string Message;
            public string Detail;
        }

        public static string GetMessage(GfsError code, object[] paramList)
        {
            if (!_list.ContainsKey(code))
            {
                return _list[GfsError.UNKNOWN].Message + "(code=" + code.ToString() + ")";
            }
            string message = _list[code].Message;
            string detail = _list[code].Detail;
            if (detail != null)
            {
                if (paramList == null)
                {
                    paramList = new object[0];
                }
                try
                {
                    message += " ";
                    message += string.Format(detail, paramList);
                }
                catch (FormatException fe)
                {
                    message += " " + detail;
                    System.Diagnostics.Debug.Assert(false);
                    ProgramUtility.OutputLogLine(fe.ToString());
                    message += " " + fe.Message;
                }
            }
            return message;
        }

    }

}
